package com.dongdong.service.Impl;

import cn.hutool.core.util.ObjUtil;
import com.dongdong.mapper.TbUserMapper;
import com.dongdong.model.TbUser;
import com.dongdong.model.TbUserExample;
import com.dongdong.result.Result;
import com.dongdong.service.UserService;
import com.dongdong.utils.ShiroUtils;
import com.dongdong.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private TbUserMapper tbUserMapper;


    @Autowired
    private JavaMailSender javaMailSender;



    // 发件人
    String from = "13572462947@163.com";
    // 收件人
    String toUser = "13572462947@163.com";
    // 发送的标题
    String subejct = "测试邮件发送标题";
    // 发送的正文
    String text = "测试邮件发送正文内容。。。。";

    // 设置邮件内容
    String html = "<a href='https://www.baidu.com'>百度一下</a>";


    @Override
    public int addUser(UserVO userVO) {
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(userVO,tbUser);
        String message = userVO.getMsg();
        String phone = userVO.getPhone();

        String code1 = (String) redisTemplate.opsForValue().get(phone);
        String code2 = (String) redisTemplate.boundValueOps(phone).get();

        if(!message.equals(code1)){
            return 11;  //验证码错误
        }

        String username = userVO.getUsername();
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        if(tbUsers.size()!=0){
            return 12;  //重名
        }
        String salt = ShiroUtils.generateSalt(8);
        String password = ShiroUtils.encryptPassword("md5", userVO.getPassword(), salt, 5);
        tbUser.setPassword(password);
        tbUser.setSalt(salt);
        tbUser.setUpdated(new Date());
        return tbUserMapper.insertSelective(tbUser);
    }

    @Override
    public TbUser selectUserByUsername(String username) {
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        return tbUsers == null||tbUsers.size()==0?null:tbUsers.get(0);
    }


    @Override
    public TbUser queryUserByEmail(String email) {
        TbUserExample example = new TbUserExample();
        example.createCriteria().andEmailEqualTo(email);

        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);

        return (tbUsers.size() > 0 && ObjUtil.isNotEmpty(tbUsers))? tbUsers.get(0):null;
    }

    @Override
    public void resetPassword(String username, String password) {
        //拿着用户名去数据库查询用户信息
        TbUserExample userExample = new TbUserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<TbUser> userList = tbUserMapper.selectByExample(userExample);
        TbUser user = userList.size() > 0 ? userList.get(0):null;
        //生成盐
        String salt = ShiroUtils.generateSalt(8);
        //生成密文
        String encryptPassword = ShiroUtils.encryptPassword("md5", password, salt, 5);
        user.setSalt(salt);
        user.setPassword(encryptPassword);
        user.setUpdated(new Date());

        //进行修改操作
        tbUserMapper.updateByPrimaryKeySelective(user);

    }



    @Override
    public void sendHtmlAndAttachmentMail() {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);

        try {
            helper.setFrom(from);
            helper.setTo(toUser);
            helper.setSubject(subejct);
            helper.setText(text);

            helper.setText(html, true);
            javaMailSender.send(mimeMessage);


        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }



    @Override
    public void sendHtmlAndAttachmentFileMail() {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;


        try {
            helper = new MimeMessageHelper(mimeMessage, true);

            helper.setFrom(from);
            helper.setTo(toUser);
            helper.setSubject(subejct);
            helper.setText(text);

            helper.setText(html, true);

            File file1 = new File("D:\\dongke\\JAVA6_2306课程\\15技术分享\\2、javamail\\redme.txt");
            helper.addAttachment(file1.getName(), file1);
            javaMailSender.send(mimeMessage);


        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }


    }



}
